package com.moss.cloud.base.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.base.api.model.from.BaseFrom;
import com.moss.cloud.base.api.model.from.FlowGroupFrom;
import com.moss.cloud.base.api.model.from.FlowGroupParam;
import com.moss.cloud.base.biz.mapper.FlowGroupMapper;
import com.moss.cloud.base.biz.model.FlowGroup;
import com.moss.cloud.base.biz.service.IFlowGroupService;
import com.moss.cloud.common.base.utils.PageUtil;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.common.core.utils.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 流程分组业务实现
 *
 * @author 瑾年
 * @date 2024-02-04
 */
@Slf4j
@Service
public class FlowGroupServiceImpl extends ServiceImpl<FlowGroupMapper, FlowGroup> implements IFlowGroupService {

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Override
    public IPage<FlowGroup> page(FlowGroupParam param) {
        Page<FlowGroup> page = PageUtil.createPage(param.getCurrent(), param.getSize());
        return this.page(page, new QueryWrapper<FlowGroup>().lambda()
                .like(StringUtils.isNotBlank(param.getGroupName()), FlowGroup::getGroupName, param.getGroupName())
                .eq(FlowGroup::getTenantId, UserContextHolder.getTenantId()));

    }
    /**
     * 列表查询
     *
     * @param param
     * @return
     */
    @Override
    public List<FlowGroup> groupList(FlowGroupParam param) {
        return this.list(new QueryWrapper<FlowGroup>().lambda()
                .like(StringUtils.isNotBlank(param.getGroupName()), FlowGroup::getGroupName, param.getGroupName())
                .eq(FlowGroup::getTenantId, UserContextHolder.getTenantId()));

    }

    /**
     * 新增
     *
     * @param from
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveFlowGroup(FlowGroupFrom from) {
        FlowGroup flowGroup = new FlowGroup();
        BeanUtils.copyProperties(from, flowGroup);
        flowGroup.setTenantId(UserContextHolder.getTenantId());
        return this.save(flowGroup);
    }

    /**
     * 更新
     *
     * @param from
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(FlowGroupFrom from) {
        BooleanHandel.trueThrow(Objects.isNull(from.getId())).throwMessage(SystemErrorType.PRIMARY_KEY);
        FlowGroup flowGroup = this.getById(from.getId());
        this.checkData(flowGroup);
        FlowGroup model = new FlowGroup();
        BeanUtils.copyProperties(from, model);
        return this.updateById(model);
    }


    /**
     * 删除
     *
     * @param baseFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(BaseFrom baseFrom) {
        FlowGroup flowGroup = this.getById(baseFrom.getId());
        this.checkData(flowGroup);
        return this.removeById(baseFrom.getId());
    }

    /**
     * 验证数据所属权
     *
     * @param flowGroup
     */
    private void checkData(FlowGroup flowGroup) {
        BooleanHandel.trueThrow(Objects.isNull(flowGroup)).throwMessage(SystemErrorType.DATE_EMPTY);
        BooleanHandel.trueThrow(!flowGroup.getTenantId().equals(UserContextHolder.getTenantId())).throwMessage(SystemErrorType.NOT_BELONG_TENANT);
    }

}
